package com.qyc.Microservice.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qyc.Microservice.dto.pojo.User;
import com.qyc.Microservice.dto.result.ResponseResult;
import com.qyc.Microservice.dto.result.ResultCode;
import com.qyc.Microservice.service.UserService;

@RestController
@RequestMapping("/JPA")
@ResponseResult
public class JPAController {
	
	@Autowired
	private UserService userService;
	
	@PostMapping("/save")
	public User save(User user) {
		return userService.save(user);
	}

	@GetMapping("/findByNameAndAge" )
	public User findByNameAndAge(@RequestParam(name="name") String name, @RequestParam(name="age") Integer age) {
		return userService.findByNameAndAge(name, age);
	}
	
	@GetMapping("/findByAge")
	public List<User> getUserByAge(User user,@RequestParam(name="pageSize") int pageSize,@RequestParam(name="pageNum") int pageNum){
		return userService.getUserByAge(user.getAge(),pageSize,pageNum);
	}
	@GetMapping("/findFirstByOrderByAgeAsc")
	public User findFirstByOrderByAgeAsc() {
		return userService.findFirstByOrderByAgeAsc();
	}
	
	@GetMapping("/findFirst3ByAge")
	public List<User> findFirst3ByAge(User user){
		return userService.findFirst3ByAge(user.getAge());
	}
	
	@GetMapping("/getByNameAndAge")
	public User getByNameAndAge(User user) {
		return userService.getByNameAndAge(user.getName(), user.getAge());
	}
	
	@GetMapping("/changePwd")
	public void changePwd(User user,@RequestParam(name="oldPwd") String oldPwd) {
		userService.changePwd(user.getName(), oldPwd, user.getPassword());
	}
	
	@GetMapping("/findByNameLike")
	@ResponseResult
	public List<User> findByNameLike(@RequestParam(name="name") String name){
		return userService.findByNameLike("%"+name +"%");
	}
	
	@GetMapping("/findAllEnum")
	public Map<Integer, String> findAllEnum(){
		return ResultCode.findAllEnum();
	}
}
